Free for noncommercial use. Contact me for a simple cheap license if you wish to include any or all of these as part of a software solution that you sell. I can also customize the package to include only specific commands as needed.
This AppleScript Scripting Addition provides AppleScript and all scriptable and Apple Event aware applications with added functionality. To examine the AppleScript syntax of each of these commands, simply drag Jon’s Commands onto the Script Editor or use the Open Dictionary command to open it. To install it, simply drop it into the Scripting Additions folder which is inside your Extensions folder.
Jon’s Commands includes these commands:
deleteFile - delete a file or files
renameFile - rename a file
moveFile - move a file or files to a different folder
copyFile - copy a file or list of files
sound volume - get the volume setting
set sound volume to - set the volume setting
clipboard info - get a list of data on the clipboard
set the clipboard to - put data on the clipboard
the clipboard - get data from the clipboard
execute FKEY - run an FKEY resource
screen list - describe the monitor configuration
finder selection - return the Finder’s selection
keys pressed - get a list of pressed keys
machine environment - get info about the machine
play sound - play sound resources, files and descriptors
run script resource - run an ‘Scpt’ or ‘scpt’ resource
free memory - return the free memory available
the ticks - return the current value of tickCount
walk folders - walk folders and run a script on each file
set cursor to - provide feedback with the cursor
AE user interaction level - control the interaction level
Jon’s Commands includes these objects:
picture - for getting them off the clipboard «class PICT»
sound - for getting them off the clipboard & playing them «class snd »
screen info - for describing the monitors
environment info - for describing the machine
Jon’s Commands includes these coercions:
string to file specification - ‘TEXT’ to ‘fss ’
styled text to file specification - ‘STXT’ to ‘fss ’
international text to file specification - ‘itxt’ to ‘fss ’
Notes for specific commands:
Finder Selection:
This Addition gets the Finder’s selected files and folders. This only works with Finder 7.0, 7.0.1, 7.1 & 7.1.1 (including the scriptable Finder). Using it with any other Finder could cause it to fail. Hopefully it won’t crash, but it probably won’t work. I only check for the system version (Gestalt doesn’t tell the Finder version, only the System version) so using a different Finder could definately cause problems.
You can place script applications in the Apple Menu Items folder or with either Jens Alfke’s ScriptMenu or Leonard Rosenthal’s OSA Menu script menus and use the finder selection function to have your script operate on the selected files. Way cool.
CopyFile, DeleteFile, MoveFile & RenameFile:
These commands work on files and lists of files. You can use file references (the file class), strings with the pathname or aliases to specify the files and folders for these commands.
DeleteFile is permanent and immediate. Be very careful with it. I recommend using “moveFile foo to (path to trash)” or the FinderLib if you are at all worried or inclined toward cautiousness.
MoveFile only moves files around on the disk they are on. It does not copy between disks. It cannot rename or replace files either. The destination is a folder, which you can specify either with the file class, an alias or as a pathname string.
CopyFile uses temporary memory if available and tries to copy the file in 1 pass for maximum speed. The source may be either a file or list of files. The destination may be either a file or folder to copy to.
execute FKEY:
This loads the FKEY resource and jumps to it. It does not press command-shift-N, so it will not trigger QuicKeys macros or any function which patches things to run at FKEY-like key combinations.
keys pressed & machine information:
Keys Pressed returns the GetKey info in the form of a list of key names in string form.
Machine Information returns a keyworded record object of common machine information from Gestalt.
Both of these use STR# resources if you are inclined to localize them. The machine info is a subset of the available Gestalt selectors. If you have need of others, let me know and we might be able to include them in a future version, although you can get raw information for any gestalt selector directly.
clipboard info, the clipboard & set the clipboard to:
All of these can manage multiple data types. Styled text is the default data type, with plain text (known as string) as the backup. International text is also supported, as are a lot of other data formats. Almost any resource type can be manipulated in AppleScript as an AEDesc in applications. Pictures and sounds are also popular to move around.
sound volume & set sound volume to:
Both of these use a number between 0 and 7 for the volume, just like everyone else.
play sound:
This can play sound (‘snd ’) resources installed in the system or the current open files by either name or id number. It can also play sound resources out of files and out of AppleScript variables (typically accessed via the clipboard). You cannot interrupt a sound in progress in this version and the sounds are played synchronously. This command is a small improvement on Donald’s sample code from the Language Reference Manual.
run script resource:
This matches the AppleScript Run Script command but works on resources instead of files. This allows you to bundle several scripts (typically from other OSA components such as QuicKeys) into a single script file or application. I’ve used it to place the ‘scpt’ resources from QuicKeys scripts into an applet and run them from there.
There is a problem with having more than 1 ‘scpt’ resource per file and that is the Script Editor. It finds the script to display by using Get1IndResource('scpt', 1) which can return the wrong resource if you have several. If you are literate with ResEdit you can make sure the proper script resource is last in the file, but that’s less certain than the other technique I recommend, which is to change the script resource’s type to ‘Scpt’ which is not recognized by the Script Editor. You need to copy the script resource as hex data and paste it into a new empty ‘Scpt’ resource. This way allows no conflicts. Another solution is to use my RunScript MPW tool which allows you to create script resources of any type and id. This makes it simple to create a script application which combines AppleScript and QuicKeys scripts. You can even record a QuicKeys script, decompile it with RunScript, edit it and recompile it as a piece of a script application.
Yet another option is simply to record a QuicKeys script, copy it and paste the text into your AppleScript script with the “run script foo in QuicKeys” command with foo as your text or a variable with text in it. This works well with simple scripts and doesn’t require this osax. ;) Running a compiled script resource should be marginally faster though. I also had some mysterious problems running text variables with the Run Script osax.
You can reference script resources by name or number.
free memory:
This returns the current value of the function FreeMem. It shows the amount of free memory available in the current application’s current heap. Useful for finding memory leaks.
the ticks:
This returns the current value of TickCount. This is useful for timing functions from scripts. Unfortunately, this opens the resource file for Jon’s Commands every time and thus affects your timing slightly, making it more useful for large timings where the overhead is less instead of small ones.
walk folders:
This makes file processors very simple. It takes a list of files and folders and a script. It then walks through them all and sends each file to an open handler in the script. Save this script as a droplet and see how it works:
on open (theFiles)
script foo
on open (theFile)
display dialog theFile as string
end open
end script
walk folders theFiles with script foo
end open
In addition, there are 2 boolean parameters, “using files” and “using folders” plus the “only using files of type” parameter. “using files” defaults to true and causes all files to be run through the script. “using folders” defaults to false and causes all folders to be run through the script. You can set them to true or false by using “with using folders” and/or “without using files”. “only using files of type” parameter allows you to specify one or more file types that will be operated on. Only files of the types specified will be sent to your script parameter. The types are 4 character strings with the obscure codes you will have to find out about. Normal choices are things like “APPL”, “osax” and “TEXT”.
set cursor to:
You can use the five standard cursors; arrow cursor, watch cursor, I beam cursor, cross cursor & plus cursor, plus a busy cursor which spins. You can also specify the name or number of a CURS resource or, using the “with cursor list” parameter, an acur resource. Subsequent calls with the “busy cursor” will increment this cursor one step.
The busy cursor keeps an acur and all the CURS resources in memory between calls. The “with cursor list” parameter is only necessary on the first call and will reinitialize the cursor if always supplied (i.e. the cursor will not spin). You can clear this memory by setting the cursor to something other than a busy cursor (i.e. watch cursor or arrow cursor).
This does not support color cursors yet.
AE user interaction level:
This allows you to get and set the AppleEvent manager’s user interaction level. This way to can allow interaction from remote machines or disallow interaction from local scripts. In order for this to work, the program which is doing the interaction must explicitly check if interaction is allowed. Hopefully all scriptable applications do.
This returns the current state and then optionally sets it to the new state.
The only drawback with this command is that you cannot turn off user interaction in the current application from a script run by OSA Menu. Since the lowest level is “AE interact with self” and a script running by OSA Menu is sending events to the same application, this is always allowed. Applications need to support a “never interact” mode explicitly. Apple’s PhotoFlash does. (shameless plug)
Coercions:
string to file specification
styled text to file specification
international text to file specification
These coercions allow you to use strings where a program or osax expects a file specification. Note that the AppleScript class “file” is really an object reference, not simply an FSSpec, although it can be coerced to one. You can create an FSSpec by using this command: “set x to "disk:folder:file" as «class fss »”.
Version History:
1.0b1 - first public release
1.0b2 - fixed key name in “keys pressed” for PowerBook keyboard.
1.0b3 - added 7.1.1 to version check for “finder selection”.
1.0b4 - added “run script resource” command.
1.0b5 - added “free memory” and “the ticks” commands.
1.0 - official release.
1.1b1 - added “copyFile” command. made “moveFile”, “deleteFile”, “renameFile” & “copyFile” accept lists of files, aliases or pathnames. added string to file specification coercion.
1.1b2 - fixed bug in “set clipboard to” which could cause changes to the clipboard to be unnoticed and fixed a really minor bug in “execute FKEY” which passed a dirty PC to the FKEY in 24 bit mode.
1.1b3 - added “walk folders” command, made “finder selection” return aliases for AS 1.0 compatibility and made “screen list” work without color QuickDraw.
1.1 - official release
1.1.1b1 - fixed “machine environment” property name conflict with “machine” by renaming it “machine type” and actually implemented the “cpu type” property. Also fixed the sample script for “walk folders” above.
1.1.1b2 - fixed “execute fkey” error handling, twiddled clipboard code to remove debugger calls & clean up the code a bit, fixed “finder selection” when items were on desktop of volumes other than the startup volume & made “walk folders” send an alias instead of a file specification for compatibility with AS 1.0.
1.1.1b3 - fixed error handler in “play sound”.
1.1.1 - official release
1.1.2b1 - made string to fsspec coercion more picky so that it doesn’t confuse the Run Script osax.
1.1.2 - official release
1.1.3b1 - recompiled with debugger breaks off. Ooops.
1.1.3 - official release
1.2b1 - added “set cursor to” command.
1.2b2 - added “AE user interaction level” function.
1.2 - official release
1.3b1 - added “only using files of type” parameter to “walk folders” command.
1.3 - official release
1.3.1b1 - made aete unpurgeable to avoid Script Editor dictionary bug.
1.3.1 - official release
Feel free to send money or to write with questions, comments or suggestions.